package com.sky.controller.admin;

import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜品管理
 */
@RestController("adminDishController")
@Slf4j
@Api(tags = "菜品管理")
@RequestMapping("/admin/dish")
public class DishController {
    private final DishService dishService;

    @Autowired
    public DishController(DishService dishService) {
        this.dishService = dishService;
    }

    /**
     * 新增菜品
     *
     * @param dishDTO dishDTO
     * @return Result
     */
    @PostMapping
    @ApiOperation("新增菜品")
    @CachePut(cacheNames = "dish", key = "#dishDTO.id")
    public Result<?> saveDish(@RequestBody DishDTO dishDTO) {
        log.info("执行 新增菜品 接口: {}", dishDTO);
        dishService.saveDishWitchFlavor(dishDTO);
        return Result.success();
    }


    /**
     * 菜品批量删除
     *
     * @param ids 删除的id集合
     * @return Result
     */
    @DeleteMapping
    @ApiOperation("批量删除菜品")
    @CacheEvict(cacheNames = "dish", allEntries = true)
    public Result<?> deleteBatch(@RequestParam List<Long> ids) {
        log.info("执行 菜品批量删除 接口:{}", ids);
        dishService.deleteBatch(ids);
        return Result.success();
    }


    /**
     * 根据菜品id修改对饮信息
     *
     * @param dishDTO dishDTO
     * @return Result
     */
    @PutMapping
    @ApiOperation("修改菜品")
    @CacheEvict(cacheNames = "dish", key = "#dishDTO.id")
    public Result<?> updateDish(@RequestBody DishDTO dishDTO) {
        log.info("执行 修改菜品 接口: {}", dishDTO);
        dishService.updateDishWithFlavor(dishDTO);
        return Result.success();
    }

    /**
     * 菜品启售停售
     * @param status 菜品售卖状态
     * @param dishId 菜品id
     * @return Result
     */
    @PostMapping("/status/{status}")
    @ApiOperation("菜品启售停售")
    @CacheEvict(cacheNames = {"dish","setmealWitchDishCache"}, allEntries = true)
    public Result<?> startOrStopDish(@PathVariable Integer status, @RequestParam("id") Long dishId) {
        log.info("执行 菜品起售停售 业务：id: {},status: {}",dishId,status);
        dishService.startOrStopDish(status, dishId);
        //清除缓存菜品
        return Result.success();
    }


    /**
     * 分页查询菜品
     *
     * @param dishPageQueryDTO 分页查询菜品的DTO
     * @return 带有分页信息的Result
     */
    @GetMapping("/page")
    @ApiOperation("分页查询菜品")
    public Result<PageResult> pageQueryDish(DishPageQueryDTO dishPageQueryDTO) {
        log.info("执行 菜品分页查询 接口：{}", dishPageQueryDTO);
        PageResult page = dishService.pageQueryDish(dishPageQueryDTO);
        return Result.success(page);
    }


    /**
     * 根据分类id查询菜品
     *
     * @param categoryId 分类id
     * @return Result
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    @Cacheable(cacheNames = {"dishCache"}, key = "#categoryId")
    public Result<List<DishVO>> listDish(Long categoryId) {
        log.info("执行 根据分类id查询菜品 接口 :{}",categoryId);
        Dish dish = new Dish();
        dish.setCategoryId(categoryId);
        dish.setStatus(StatusConstant.ENABLE);//查询起售中的菜品

        List<DishVO> list = dishService.listDishWithFlavor(dish);

        return Result.success(list);
    }

    /**
     * 根据菜品id查询信息
     *
     * @param dishId 菜品id
     * @return Result
     */
    @GetMapping("/{dishId}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> getByDishId(@PathVariable Long dishId) {
        log.info("执行 根据id查询菜品 接口: {}",dishId);
        DishVO dishVO = dishService.getByDishIdWithFlavor(dishId);
        return Result.success(dishVO);
    }



}
